<?php

//每天执行2次
require_once dirname(__FILE__) . '/autoload.php';
require_once dirname(__FILE__) . '/curl_proxy.php';

$timeStart = time();

$GlobalResultData = array();

$ShouRuData = array();
if (file_exists(dirname(__FILE__) . '/report.csv')) {
    $fp = fopen(dirname(__FILE__) . '/report.csv', 'r');
    $INDEX = 0;
    while ($dataline = fgets($fp)) {
        if (!$INDEX) {
            $INDEX++;
            continue;
        }
        $dataline = explode("\t", $dataline);
        $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
        $arrDoamin = explode('.', $dataline[0]);
        $dataline[0] = $arrDoamin[1] . '.' . $arrDoamin[2];
        $ShouRuData[$dataline[0]] = preg_replace('#\s+#', '', $dataline[6]);
        $INDEX++;
    }
    fclose($fp);
}


$s = '';
if (file_exists(dirname(__FILE__) . '/domainlist.txt')) {
    $s = file_get_contents(dirname(__FILE__) . '/domainlist.txt');
}

$domainlist = array_filter(explode('<br />', nl2br(trim($s))));
foreach ($domainlist as &$ds) {
    $ds = preg_replace('#\s+#', '', $ds);
}unset($ds);
$domainlist = array_filter($domainlist);

$locallockfile = dirname(__FILE__) . '/locklist.php';
if (!file_exists($locallockfile)) {
    log::e('!file_exists($locallockfile)', true, true);
    die();
}
$lockdomains = json_decode(file_get_contents($locallockfile), true);
$arrUniqDomainDataUniq = array();

$arrUniqDomainDataWWW = array_merge($domainlist, $lockdomains);
foreach ($arrUniqDomainDataWWW as $domainALL) {
    $arrDInfo = explode('.', $domainALL);
    $length = count($arrDInfo);
    $extk = $length - 1;
    $dok = $length - 2;
    $domain = $arrDInfo[$dok] . '.' . $arrDInfo[$extk];
    if (is_numeric($arrDInfo[$extk])) {
        log::w($domain);
        continue;
    }
    $arrUniqDomainDataUniq[] = $domain;
}

$arrUniqDomainDataUniq = array_filter($arrUniqDomainDataUniq);
$arrUniqDomainDataUniq = array_unique($arrUniqDomainDataUniq);

$TotalCount = count($arrUniqDomainDataUniq);
$WarningCount = 0;

$HTMLBODY = '';
$HTMLBODY.= '<b>whois.com域名过期时间统计(' . $TotalCount . '个)：</b></div>
          <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY.='<tr height="35">
    <td width="150">域名&nbsp;</td>
    <td width="100">注册日期&nbsp;</td>
    <td width="100">过期时间&nbsp;</td>
    <td width="100">DNS&nbsp;</td>
    <td width="100">距离过期时间</td></tr>
    ';

@unlink(dirname(__FILE__) . '/Domain-List.txt');
foreach ($arrUniqDomainDataUniq as $domain) {
    file_put_contents(dirname(__FILE__) . '/Domain-List.txt', $domain . PHP_EOL, 8);
}
foreach ($arrUniqDomainDataUniq as $domain) {
    file_put_contents(dirname(__FILE__) . '/Domain-List.txt', $domain . PHP_EOL, 8);
    $arrDateTime = domain_exp_check($domain);
    //距离过期时间
    $time = strtotime($arrDateTime['expdate']) - time();
    $days = floor($time / 86400);
    if ($days <= 90) {
        $color = ' color="red" ';
        $WarningCount++;
    } else {
        $color = '';
    }
    if ($days >= 10000) {
        $arrDateTime = domain_exp_check($domain);
        $time = strtotime($arrDateTime['expdate']) - time();
        $days = floor($time / 86400);
        if ($days <= 90) {
            $color = ' color="red" ';
            $WarningCount++;
        } else {
            $color = '';
        }
        if ($days >= 10000) {
            $arrDateTime = domain_exp_check($domain);
            $time = strtotime($arrDateTime['expdate']) - time();
            $days = floor($time / 86400);
            if ($days <= 90) {
                $color = ' color="red" ';
                $WarningCount++;
            } else {
                $color = '';
            }
        }
        if ($days >= 10000) {
            $arrDateTime = domain_exp_check($domain);
            $time = strtotime($arrDateTime['expdate']) - time();
            $days = floor($time / 86400);
            if ($days <= 90) {
                $color = ' color="red" ';
                $WarningCount++;
            } else {
                $color = '';
            }
        }
        if ($days >= 10000) {
            $arrDateTime = domain_exp_check($domain);
            $time = strtotime($arrDateTime['expdate']) - time();
            $days = floor($time / 86400);
            if ($days <= 90) {
                $color = ' color="red" ';
                $WarningCount++;
            } else {
                $color = '';
            }
        }
        if ($days >= 10000) {
            $arrDateTime = domain_exp_check($domain);
            $time = strtotime($arrDateTime['expdate']) - time();
            $days = floor($time / 86400);
            if ($days <= 90) {
                $color = ' color="red" ';
                $WarningCount++;
            } else {
                $color = '';
            }
        }
    }

    if ($color == ' color="red" ') {

        $GlobalResultData['red'][] = '<tr height="35">
    <td width="150"><font' . $color . '>' . $domain . '</font>[' . $ShouRuData[$domain] . ']</td>
    <td width="100">' . $arrDateTime['regdate'] . '</td>
    <td width="100"><font' . $color . '>' . $arrDateTime['expdate'] . '</font></td>
    <td width="100">' . $arrDateTime['dnsserver'] . '</td>
    <td width="100"><font' . $color . '>' . $days . '</font>&nbsp;天</td></tr>
    ';
    } else {
        $GlobalResultData['green'][] = '<tr height="35">
    <td width="150"><font' . $color . '>' . $domain . '</font>[' . $ShouRuData[$domain] . ']</td>
    <td width="100">' . $arrDateTime['regdate'] . '</td>
    <td width="100"><font' . $color . '>' . $arrDateTime['expdate'] . '</font></td>
    <td width="100">' . $arrDateTime['dnsserver'] . '</td>
    <td width="100"><font' . $color . '>' . $days . '</font>&nbsp;天</td></tr>
    ';
    }
}

foreach ($GlobalResultData['red'] as $html) {
    $HTMLBODY.=$html;
}

foreach ($GlobalResultData['green'] as $html) {
    $HTMLBODY.=$html;
}

$HTMLBODY.='</table>';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$arrUsers = array(
    'think889977@sina.com',
);

$timeEnd = time();
$HTMLBODY.='<hr />耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
$HTMLBODY.=implode('<br />', $arrUniqDomainDataUniq);

post_email('[警告:' . $WarningCount . ']每日域名自动检测报告[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);

/**
 * 域名过期查询
 * @param string $domain
 * @return boolean
 */
function domain_exp_check($domain) {
    $arr = explode('.', $domain);
    $length = count($arr);
    $extk = $length - 1;
    $dok = $length - 2;
    $domain = $arr[$dok] . '.' . $arr[$extk];
    if (!in_array($arr[$extk], array('com', 'cn', 'net', 'org', 'info'))) {
        return false;
    }
    $whoisurl = 'http://www.whois.com/whois/' . $domain;
    //$s = file_get_contents($whoisurl);
    $s = curl_string($whoisurl);
    if (!$s) {
        $s = curl_string($whoisurl);
    }
    if (!$s) {
        $s = curl_string($whoisurl);
    }

    //$s = preg_replace('#\s+#', '', $s);
    $pexpdate = '#<br>Expiration Date:(.*)<br>#isU';
    preg_match_all($pexpdate, $s, $dexp);
    $regdate = '#<br>Created On:(.*)<br>#isU';
    preg_match_all($regdate, $s, $dreg);
    $dns = '#<br>Name Server:(.*)<br>#isU';
    preg_match_all($dns, $s, $ddns);
    log::n($domain . ':' . preg_replace('#\s+#', '', $dexp[1][0]));
    //print_r($dexp);
    return array('regdate' => $dreg[1][0], 'expdate' => preg_replace('#\s+#', '', $dexp[1][0]), 'dnsserver' => $ddns[1][0]);
}

?>